<?php
/**
 * Created by PhpStorm.
 * User: 17828
 * Date: 2018/10/15
 * Time: 13:36
 */

namespace app\master\controller;

use think\Db;
use think\Exception;

class Payment extends Base
{
    //薪酬设置平台驾驶员
    public function platformDriver()
    {
        if (request()->isGet()) {
            return $this->fetch('platformdriver');
        }
        $page = input("page");
        $limit = input("limit");
        $where = [
            'd.type' => 2,
        ];
        $list = model('Drivelevel')->getDrivePrice($where, $page, $limit);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['drivelevel_price'] = $list['data'][$k]['drivelevel_price'] / 100;
            $list['data'][$k]['drivelevel_priceday'] = $list['data'][$k]['drivelevel_priceday'] / 100;
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //薪酬设置临时驾驶员
    public function temporaryDriver()
    {
        if (request()->isGet()) {
            return $this->fetch('temporarydriver');
        }
        $page = input("page");
        $limit = input("limit");
        $where = [
            'd.type' => 3,
        ];
        $list = model('Drivelevel')->getDrivePrice($where, $page, $limit);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['drivelevel_price'] = $list['data'][$k]['drivelevel_price'] / 100;
            $list['data'][$k]['drivelevel_priceday'] = $list['data'][$k]['drivelevel_priceday'] / 100;
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //薪酬设置个人车辆
    public function personalCar()
    {
        if (request()->isGet()) {
            return $this->fetch('personalcar');
        }
        $page = input('page');
        $limit = input('limit');
        $where = [
            'v.type' => 2
        ];
        $field = "v.vehicle_rent_id,v.vehicle_price,v.vehicle_priceday,concat(ca.cartype_what,c.cartype_what) as name";
        $list = model('Vehiclerent')->getVehicleRentList($field, $page, $limit, $where);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['vehicle_price'] = $list['data'][$k]['vehicle_price'] / 100;
            $list['data'][$k]['vehicle_priceday'] = $list['data'][$k]['vehicle_priceday'] / 100;
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //薪酬设置个人车辆及驾驶员
    public function personalCarAndDrive()
    {
        //车辆级驾驶员价格设置
        if (request()->isGet()) {
            return $this->fetch('personalcaranddrive');
        }
        $field = "concat(ca.cartype_what,c.cartype_what) as name,dl.drive_level,dc.driver_car_price,dc.driver_car_priceday,dc.driver_car_id";
        $where = [
            'dc.type' => 2,
        ];
        $list = model('DriverCar')->getDriveCarPrice($field, $where);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['driver_car_price'] = $list['data'][$k]['driver_car_price'] / 100;
            $list['data'][$k]['driver_car_priceday'] = $list['data'][$k]['driver_car_priceday'] / 100;
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //清空驾驶员价格
    public function delDrivePrice()
    {
        $drivelevel_id = input('drivelevel_id');
        $data = db('drivelevel')->where('drivelevel_id', $drivelevel_id)->update(['drivelevel_price' => null, 'drivelevel_priceday' => null]);
        if ($data) $this->ajaxReturn(200, "清空成功", url("Pricemanage/driveprice"));
        else $this->ajaxReturn(603, "清空失败");
    }

    //添加平台驾驶员等级
    public function addDriveLevelPrice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $data = db('driving_licence')->where('status', 1)->select();
            $this->assign('data', $data);
            $this->assign('type', 2);
            return $this->fetch("adddrivelevelprice");
        }
        $this->addDrive();
    }

    //添加临时驾驶员等级
    public function addDrivePrice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $data = db('driving_licence')->where('status', 1)->select();
            $this->assign('data', $data);
            $this->assign('type', 3);
            return $this->fetch("adddriveprice");
        }
        $this->addDrive();
    }

    //私有的添加驾驶员等级方法
    private function addDrive()
    {
        $type = input('type');
        $drivelevel_id = input('driving_licence_id');
        $drivelevel_price = input('drivelevel_price');
        $drivelevel_priceday = input('drivelevel_priceday');
        $count = db('drivelevel')->where(['driving_licence_id' => $drivelevel_id, 'type' => $type])->count();
        if ($count) $this->ajaxReturn(603, "驾驶证等级已存在！");
        $drivelevel = [
            "driving_licence_id" => $drivelevel_id,
            "drivelevel_price" => $drivelevel_price * 100,
            "drivelevel_priceday" => $drivelevel_priceday * 100,
            "type" => $type,
        ];
        $update = db("drivelevel")->insert($drivelevel);
        if ($update) $this->ajaxReturn(200, "添加成功");
        else $this->ajaxReturn(603, "添加失败");
    }

    //修改驾驶员租金
    public function updDrivePrice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $info = db("drivelevel")->find(input("drivelevel_id"));
            $info['drivelevel_price'] = $info['drivelevel_price'] / 100;
            $info['drivelevel_priceday'] = $info['drivelevel_priceday'] / 100;
            $data = db('driving_licence')->where('status', 1)->select();
            $this->assign('data', $data);
            $this->assign("info", $info);
            return $this->fetch("updprice");
        }
        $data = input('post.');
        $upd = [
            "driving_licence_id" => $data['driving_licence_id'],
            "drivelevel_price" => $data['drivelevel_price'] * 100,
            "drivelevel_priceday" => $data['drivelevel_priceday'] * 100,
        ];
        $update = db("drivelevel")->where('drivelevel_id', $data['drivelevel_id'])->update($upd);
        if ($update) {
            $this->ajaxReturn(200, "修改成功");
        } else {
            $this->ajaxReturn(603, "修改失败");
        }
    }


    //添加车型价格
    public function addCarTypePrice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $this->assign('cartype', $cartype);

            return $this->fetch('addcartypeprice');
        }
        $data = input();
        $data['vehicle_price'] = $data['vehicle_price'] * 100;
        $data['vehicle_priceday'] = $data['vehicle_priceday'] * 100;
        $data['type'] = 2;
        $count = db('vehicle_rent')->where(['cartype_id' => $data['cartype_id'], 'type' => 2])->count();
        if ($count) $this->ajaxReturn(603, "该车型已存在！");
        $res = db('vehicle_rent')->insert($data);
        if ($res) $this->ajaxReturn(200, "添加成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //修改车型价格
    public function updCarTypePrice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $id = input('id');
            $data = db('vehicle_rent')->where('vehicle_rent_id', $id)->find();
            $data['vehicle_price'] = $data['vehicle_price'] / 100;
            $data['vehicle_priceday'] = $data['vehicle_priceday'] / 100;
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $data['pid'] = db('cartype')->where('cartype_id', $data['cartype_id'])->value('pid');
            $carTwoType = model('carmanage')->gettypelist(['pid' => ['not in', 0]]);
            $this->assign('data', $data);
            $this->assign('cartype', $cartype);
            $this->assign('carTwoType', $carTwoType);
            return $this->fetch('updcartypeprice');
        }
        $data = input();
        $data['vehicle_price'] = $data['vehicle_price'] * 100;
        $data['vehicle_priceday'] = $data['vehicle_priceday'] * 100;
        $res = db('vehicle_rent')->update($data);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //清空车型价格
    public function delCarPrice()
    {
        $id = input('id');
        $res = db('vehicle_rent')->where('vehicle_rent_id', $id)->update(['vehicle_price' => null, 'vehicle_priceday' => null]);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //添加车及驾驶员价格
    public function addCarAndDrivePrice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $data = db('driving_licence')->where('status', 1)->select();
            $this->assign('cartype', $cartype);
            $this->assign('data', $data);
            return $this->fetch('addcaranddriveprice');
        }
        $data = input();
        $data['driver_car_price'] = $data['driver_car_price'] * 100;
        $data['driver_car_priceday'] = $data['driver_car_priceday'] * 100;
        $data['type'] = 2;
        $count = db('driver_car')->where(['driving_licence_id' => $data['driving_licence_id'], 'cartype_id' => $data['cartype_id'], 'type' => 2])->count();
        if ($count) $this->ajaxReturn(603, "此类型组合已存在！");
        $res = db('driver_car')->insert($data);
        if ($res) $this->ajaxReturn(200, "添加成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //修改车及驾驶员价格
    public function updCarAndDrivePrice()
    {
        $this->view->engine->layout(false);
        if (request()->isGet()) {
            $id = input('id');
            $data = db('driver_car')->where('driver_car_id', $id)->find();
            $data['driver_car_price'] = $data['driver_car_price'] / 100;
            $data['driver_car_priceday'] = $data['driver_car_priceday'] / 100;
            $cartype = model('carmanage')->gettypelist(['pid' => 0]);
            $data['pid'] = db('cartype')->where('cartype_id', $data['cartype_id'])->value('pid');
            $carTwoType = model('carmanage')->gettypelist(['pid' => ['not in', 0]]);
            $driving = db('driving_licence')->where('status', 1)->select();
            $this->assign('driving', $driving);
            $this->assign('data', $data);
            $this->assign('cartype', $cartype);
            $this->assign('carTwoType', $carTwoType);
            return $this->fetch('updcaranddriveprice');
        }
        $data = input();
        $data['driver_car_price'] = $data['driver_car_price'] * 100;
        $data['driver_car_priceday'] = $data['driver_car_priceday'] * 100;
        $res = db('driver_car')->update($data);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //清空车及驾驶员价格
    public function delCarAndDrivePrice()
    {
        $id = input('id');
        $res = db('driver_car')->where('driver_car_id', $id)->update(['driver_car_price' => null, 'driver_car_priceday' => null]);
        if ($res) $this->ajaxReturn(200, "操作成功");
        else $this->ajaxReturn(603, "网络错误");
    }

    //平台驾驶员结算
    public function platformDriverSettlement()
    {
        $data['start_date'] = input('start_date');
        $data['end_date'] = input('end_date');
        $data['store_id'] = input('store_id');
        if (request()->isGet()) {
            $store = db('store')->field("store_id,store_name")->select();
            $this->assign('store', $store);
            $this->assign('data', $data);
            return $this->fetch('platformdriversettlement');
        }
        $where = [
            'd.drive_type' => 1,
            'c.contractcar_status' => 4,
            'dg.is_personaldriving' => 2
        ];
        if ($data['store_id']) $where['d.store_id'] = $data['store_id'];
        if ($data['start_date'] && $data['end_date']) {
            $where['c.contractcar_bigontime'] = ['between', [strtotime($data['start_date']), strtotime($data['end_date']) + 86400]];
        }
        $page = input('page');
        $limit = input('limit');
        $field = "d.drive_id,d.drive_name,drive_sex,d.drive_tel,sum(c.travel_time) as travel_time,sum(c.mileage) as mileage,s.store_name";
        $list = model('Drive')->getPlatformDriverSettlement($where, $page, $limit, $field);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //临时驾驶员结算
    public function temporaryDriverSettlement()
    {
        $data['start_date'] = input('start_date');
        $data['end_date'] = input('end_date');
        $data['store_id'] = input('store_id');
        if (request()->isGet()) {
            $store = db('store')->field("store_id,store_name")->select();
            $this->assign('store', $store);
            $this->assign('data', $data);
            return $this->fetch('temporarydriversettlement');
        }
        $where = [
            'd.drive_type' => 2,
            'c.contractcar_status' => 4,
            'dg.is_personaldriving' => 2
        ];
        if ($data['store_id']) $where['d.store_id'] = $data['store_id'];
        if ($data['start_date'] && $data['end_date']) {
            $where['c.contractcar_bigontime'] = ['between', [strtotime($data['start_date']), strtotime($data['end_date']) + 86400]];
        }
        $page = input('page');
        $limit = input('limit');
        $field = "d.drive_id,d.drive_name,drive_sex,d.drive_tel,sum(c.travel_time) as travel_time,sum(c.mileage) as mileage,s.store_name";
        $list = model('Drive')->getPlatformDriverSettlement($where, $page, $limit, $field);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //个人车辆薪酬结算
    public function carBalance()
    {
        $data['start_date'] = input('start_date');
        $data['end_date'] = input('end_date');
        $data['car_plate'] = input('car_plate');
        if (request()->isGet()) {
            $this->assign('data', $data);
            return $this->fetch('carbalance');
        }
        $page = input('page');
        $limit = input('limit');
        $where = [
            'c.car_who' => ['in', [3, 4]],
            //'c.car_status' => 1,
            'co.contractcar_status' => 4,
        ];
        $field = "c.car_id,c.car_plate,c.car_who,sum(co.travel_time) as travel_time,sum(co.mileage) as mileage,c.drive_id";
        if ($data['car_plate']) $where['c.car_plate'] = $data['car_plate'];
        if ($data['start_date'] && $data['end_date']) {
            $where['co.contractcar_bigontime'] = ['between', [strtotime($data['start_date']), strtotime($data['end_date']) + 86400]];
        }
        $list = model('Car')->getCarBalance($field, $where, $page, $limit);
        foreach ($list['data'] as $k => $v) {
            if ($list['data'][$k]['car_who'] == 3) {
                $find = db('drive')->field("drive_name,drive_sex,drive_tel")->where('drive_id', $list['data'][$k]['drive_id'])->find();
            } else {
                $find = db('member')->field("member_name as drive_name,member_sex as drive_sex,member_tel as drive_tel")->where('member_id', $list['data'][$k]['drive_id'])->find();
            }
            $list['data'][$k]['name'] = $find['drive_name'];
            $list['data'][$k]['sex'] = $find['drive_sex'];
            $list['data'][$k]['phone'] = $find['drive_tel'];
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //个人车辆及驾驶员薪酬结算
    public function personalDriving()
    {
        $data['start_date'] = input('start_date');
        $data['end_date'] = input('end_date');
        $data['car_plate'] = input('car_plate');
        if (request()->isGet()) {
            $this->assign('data', $data);
            return $this->fetch('personaldriving');
        }
        $page = input('page');
        $limit = input('limit');
        $where = [
            'dl.is_personaldriving' => 1,
            'co.contractcar_status' => 4,
        ];
        $field = "dl.driveingcar_id,c.car_plate,d.drive_name,d.drive_sex,d.drive_tel,sum(co.mileage) as mileage,sum(co.travel_time) as travel_time,dl.car_id,dl.drive_id";
        if ($data['car_plate']) $where['c.car_plate'] = $data['car_plate'];
        if ($data['start_date'] && $data['end_date']) {
            $where['co.contractcar_bigontime'] = ['between', [strtotime($data['start_date']), strtotime($data['end_date']) + 86400]];
        }
        $list = model('DrivingcarLog')->getPersonalDriving($field, $where, $page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //个人车辆工作记录
    public function carWorkLog()
    {
        $data['start_date'] = input('start_date');
        $data['end_date'] = input('end_date');
        $data['id'] = input('id');
        if (request()->isGet()) {
            $this->assign('data', $data);
            return $this->fetch('carworklog');
        }
        $page = input('page');
        $limit = input('limit');
        $where = [
            'c.car_who' => ['in', [3, 4]],
            'co.contractcar_status' => 4,
            'c.car_id' => $data['id'],
        ];
        if ($data['start_date'] && $data['end_date']) {
            $where['co.contractcar_bigontime'] = ['between', [strtotime($data['start_date']), strtotime($data['end_date']) + 86400]];
        }
        $field = "c.car_plate,d.drive_name,s.store_name,co.contractcar_company,co.mileage,from_unixtime(co.contractcar_bigontime,'%Y .%m .%d %H:%i:%s') as contractcar_bigontime,from_unixtime(co.contractcar_endtime,'%Y .%m .%d %H:%i:%s') as contractcar_endtime";
        $list = model('Contractcar')->getCarWorkLog($field, $where, $page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //个人车辆及驾驶员工作记录
    public function personalAndCarWorkLog()
    {
        $data['start_date'] = input('start_date');
        $data['end_date'] = input('end_date');
        $data['drive_id'] = input('drive_id');
        $data['car_id'] = input('car_id');
        if (request()->isGet()) {
            $this->assign('data', $data);
            return $this->fetch('personalandcarworklog');
        }
        $page = input('page');
        $limit = input('limit');
        $where = [
            'dl.drive_id' => $data['drive_id'],
            'dl.car_id' => $data['car_id'],
        ];
        if ($data['start_date'] && $data['end_date']) {
            $where['co.contractcar_bigontime'] = ['between', [strtotime($data['start_date']), strtotime($data['end_date']) + 86400]];
        }
        $field = "c.car_plate,d.drive_name,co.contractcar_company,co.mileage,from_unixtime(co.contractcar_bigontime,'%Y .%m .%d %H:%i:%s') as contractcar_bigontime,from_unixtime(co.contractcar_endtime,'%Y .%m .%d %H:%i:%s') as contractcar_endtime";
        $list = model('DrivingcarLog')->getPersonalDriving($field, $where, $page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //驾驶员工作记录
    public function driveWorkLog()
    {
        $id = input('id');
        $type = input('type');//驾驶员类型1平台2临时
        $start_date = input('start_date');
        $end_date = input('end_date');
        if (request()->isGet()) {
            $this->assign('id', $id);
            $this->assign('type', $type);
            $this->assign('start_date', $start_date);
            $this->assign('end_date', $end_date);
            return $this->fetch('driveworklog');
        }
        $where = [
            'co.drive_id' => $id,
            'd.drive_type' => $type,
            'co.contractcar_status' => 4,
            'co.contractcar_type' => ['in', [1, 2]]
        ];
        if ($start_date && $end_date) {
            $where['co.contractcar_bigontime'] = ['between', [strtotime($start_date), strtotime($end_date) + 86400]];
        }
        $page = input('page');
        $limit = input('limit');
        $field = "s.store_name,d.drive_name,from_unixtime(co.contractcar_bigontime,'%Y .%m .%d %H:%i:%s') as contractcar_bigontime,from_unixtime(co.contractcar_endtime,'%Y .%m .%d %H:%i:%s') as contractcar_endtime,contractcar_company,c.car_plate,co.mileage";
        $list = model('Contractcar')->getDriveWorkLog($field, $where, $page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //平台驾驶员签到记录
    public function signLog()
    {
        $id = input('id');
        $start_date = input('start_date');
        $end_date = input('end_date');
        if (request()->isGet()) {
            $this->assign('id', $id);
            $this->assign('start_date', $start_date);
            $this->assign('end_date', $end_date);
            return $this->fetch('signlog');
        }
        $where = [
            'sg.drive_id' => $id,
        ];
        if ($start_date && $end_date) {
            $where['sg.signin_time'] = ['between', [strtotime($start_date), strtotime($end_date) + 86400]];
        }
        $page = input('page');
        $limit = input('limit');
        $field = "s.store_name,d.drive_name,sg.signin_longitude,sg.signin_latitude,sg.type,from_unixtime(sg.signin_time,'%Y .%m .%d %H:%i:%s') as signin_time";
        $list = model('Signin')->getSignLog($field, $where, $page, $limit);
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['address'] = getAddress($list['data'][$k]['signin_longitude'], $list['data'][$k]['signin_latitude']);
        }
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //发工资
    public function payOff()
    {
        $id = input('id');
        $type = input('type');//1平台驾驶员2临时驾驶员3个人车辆4个人车辆及车
        $start_date = input('start_date');
        $end_date = input('end_date');
        $mileage = input('mileage');
        $travel_time = input('travel_time');
        if (request()->isGet()) {
            $this->assign('id', $id);
            $this->assign('type', $type);
            $this->assign('start_date', $start_date);
            $this->assign('end_date', $end_date);
            $this->assign('mileage', $mileage);
            $this->assign('travel_time', $travel_time);
            return $this->fetch('payoff');
        }
        $val = input('val');//1公里2时间
        $mode = $val == 1 ? $mileage : $travel_time;
        switch ($type) {
            case 1:
                $table = "drive";
                $where = ['drive_id' => $id];
                $p_table = "drivelevel";
                $field = 'driving_licence_id';
                $p_field = $val == 1 ? 'drivelevel_price' : 'drivelevel_priceday';
                $p_where = ['type' => 2,];
                break;
            case 2:
                $table = "drive";
                $where = ['drive_id' => $id];
                $p_table = "drivelevel";
                $field = 'driving_licence_id';
                $p_field = $val == 1 ? 'drivelevel_price' : 'drivelevel_priceday';
                $p_where = ['type' => 3,];
                break;
            case 3:
                $table = "car";
                $where = ['car_id' => $id];
                $field = 'cartype_ids';
                $p_table = "vehicle_rent";
                $p_field = $val == 1 ? 'vehicle_price' : 'vehicle_priceday';
                $p_where = ['type' => 2,];
                break;
            case 4:
                $find = db('drivingcar_log')->where('driveingcar_id', $id)->find();
                $drive_level = db('drive')->where('drive_id', $find['drive_id'])->value('driving_licence_id');
                $car_level = db('car')->where('car_id', $find['car_id'])->value('cartype_ids');
                $p_field = $val == 1 ? 'driver_car_price' : 'driver_car_priceday';
                $p_where = [
                    'driving_licence_id' => $drive_level,
                    'cartype_id' => $car_level,
                    'type' => 2
                ];
                $p_table = "driver_car";
                break;
        }
        if ($type != 4) {
            $value = db($table)->where($where)->value($field);
            if ($type == 3) $p_where['cartype_id'] = $value;
            else $p_where[$field] = $value;
        }
        $price = db($p_table)->where($p_where)->value($p_field);
        if (!$price) $this->ajaxReturn(603, '未设置该等级薪酬！');
        $total_price = $mode * $price;
        $data = [
            'start_date' => $start_date,
            'end_date' => $end_date,
            'total_price' => $total_price / 100
        ];
        $this->ajaxReturn(200, '操作成功', '', $data);
    }
}